package com.gzu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TeacherScrollableResultSet {
    public static void scrollableResultSet(Connection conn) {
        String sql = "SELECT * FROM teacher";
        try (PreparedStatement pstmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
            try (ResultSet rs = pstmt.executeQuery()) {
                // Scroll to the last row
                if (rs.last()) {
                    int totalRows = rs.getRow();
                    // Go to the second last row
                    rs.absolute(totalRows - 2);
                    System.out.println("Second last teacher ID: " + rs.getInt("id"));
                    System.out.println("Second last teacher name: " + rs.getString("name"));
                    System.out.println("Second last teacher course: " + rs.getString("course"));
                    System.out.println("Second last teacher birthday: " + rs.getString("birthday"));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}